import 'dart:async';

import 'package:flutter/material.dart';
import 'package:my_app/common/common.dart';
import 'package:my_app/home/home.dart';
import 'package:my_app/home/test.dart';

class Passport extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(body: TimeDown());
  }
}

class ButtonGroup extends StatelessWidget {
  const ButtonGroup({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final ButtonStyle buttonStyle = Common.buttonStyle;
    TextStyle buttonTextStyle = Common.buttonTextStyle;
    double buttonWidth = 230.0;

    return Container(
      padding: EdgeInsets.all(10),
      child: Flex(
        direction: Axis.horizontal,
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Container(
            width: buttonWidth,
            child: TextButton(
              style: buttonStyle,
              child: Text(
                "立即加入会员",
                style: buttonTextStyle,
              ),
              onPressed: () {
                Navigator.of(context)
                    .push(MaterialPageRoute(builder: (context) {
                  return Countdown();
                }));
                print("立即加入会员");
              },
            ),
          ),
          Container(
            width: buttonWidth,
            child: TextButton(
              style: buttonStyle,
              child: Text(
                "无会员下一步",
                style: buttonTextStyle,
              ),
              onPressed: () {
                print("无会员下一步");
              },
            ),
          ),
        ],
      ),
    );
  }
}

class TimeDown extends StatefulWidget {
  const TimeDown({Key? key}) : super(key: key);

  @override
  _TimeDownState createState() => _TimeDownState();
}

class _TimeDownState extends State<TimeDown> {
  ButtonStyle buttonStyle = Common.buttonStyle;
  TextStyle buttonTextStyle = Common.buttonTextStyle;
  Timer? _timer;
  int _count = 60;
  @override
  void initState() {
    print("state_initState");
    super.initState();
    // myTimer();
  }

  @override
  void dispose() {
    print('dispose_166');
    if (_timer != null) {
      _timer!.cancel();
      _timer = null;
    }
    super.dispose();
  }

  myTimer() {
    _timer = Timer.periodic(Duration(milliseconds: 1000), (t) {
      setState(() {
        //秒数减一，因为一秒回调一次
        _count--;
      });
      print(_count);
      if (_count <= 0) {
        setState(() {
          //秒数减一，因为一秒回调一次
          _count = 0;
        });
        t.cancel(); // 定时器内部触发销毁
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (context) {
              return Home();
            },
          ),
        );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Flex(
        direction: Axis.vertical,
        children: [
          Container(
            margin: EdgeInsets.all(20),
            height: 72,
            child: Align(
              // height: 100,
              // color: Colors.red,
              // padding: EdgeInsets.only(right: 110),
              alignment: Alignment.centerRight,
              child: TextButton(
                style: buttonStyle,
                child: Text(
                  "立即结束($_count)",
                  style: buttonTextStyle,
                ),
                onPressed: () {
                  print("立即结束");
                  Navigator.of(context).push(
                    MaterialPageRoute(
                      builder: (context) {
                        return Home();
                      },
                    ),
                  );
                },
              ),
            ),
          ),
          Expanded(
            child: Container(
              child: Image.asset("/images/pass-port/pass-port.gif"),
              // child: Image.asset("/images/pass-port/pass-port.webp"),
              // child: Image.network(
              //   "https://img-blog.csdnimg.cn/20181220223843377.gif"
              // ),
              padding: EdgeInsets.all(20),
            ),
            flex: 1,
          ),
          ButtonGroup(),
        ],
      ),
    );
  }
}
